package com.game.view.component
{
	import base.BaseObject;
	import com.game.data.constant.FrameRate;
	import com.game.engine.phys.Vector2D;
	import com.game.event.GameEventType;
	import com.game.manager.EventManager;
	import flash.geom.Point;
	import flash.text.TextField;
	
	public class MonsterAppearPoint extends BaseObject
	{
		
		public function MonsterAppearPoint()
		{
			super();
			velocity = Vector2D.zero;
			gravity = 0;
		}
		
		public var delay:Number;
		public var interval:Number;
		public var totalNum:int;
		public var enemyType:int;
		public var stopPointIdx:int;
		public var isOver:Boolean;
		private var count:int = 0;
		private var isStart:Boolean = false;
		private var currentCount:int = 0;
		private var isReady:Boolean;
		
		public function start():void
		{
			isStart = true;
			delay *= FrameRate.ANIMATE_FRAME_RATE;
		}
		
		override public function render(ratio:Number = 1):void
		{
		
		}
		
		override public function renderAnimate():void
		{
			if(!isStart)
			{
				return;
			}
			if(delay > 0)
			{
				delay--;
				return;
			}
			if (currentCount >= totalNum)
			{
				isOver = true;
				destroy();
				return;
			}
			if (count / FrameRate.ANIMATE_FRAME_RATE >= interval)
			{
				isReady = true;
			}
			if (isReady)
			{
				if (gc.pWorld.getMonsterArray().length < gc.maxMonsterPerScreen)
				{
					EventManager.getIns().dispatchEvent(GameEventType.CREATE_MONSTER,{type:enemyType,x:x,y:y});
					++currentCount;
					isReady = false;
				}
			}
			if (count++ / FrameRate.ANIMATE_FRAME_RATE >= interval)
			{
				count = 0;
			}
		}
		
		override protected function onAdded():void
		{
			CONFIG::DEBUG{
				graphics.beginFill(0xFF0000);
				graphics.drawRect(-30, -40, 60, 80);
				graphics.endFill();
				var txt:TextField = new TextField();
				txt.defaultTextFormat.align = "center";
				with (txt)
				{
					text += "wave: " + (stopPointIdx + 1).toString() + "\n";
					text += "type: " + enemyType + "\n";
					text += "num: " + totalNum;
				}
				txt.x = -txt.textWidth / 2;
				txt.y = -txt.textHeight / 2;
				addChild(txt);
			}
		}
		
		override public function destroy():void
		{
			isReadyToDestroy = true;
			removeFromParent();
		}
	}
}
